$(function () {
    //全局定义页数与总条数
    var markid = getUrlString('markid');
    var key;
    // var markid = '3b70887e5d841ff8ab7388ea5f41a5af';
    var page = -1,totalSize = 0,sort= 'time',sptypes = '',maintypes = '',maxmoney = '99999',minmoney = '1',filter = 1;
    $(document).on('click', '.all', function() {
        var buttons1 = [
            {
                text: '全部',
                bold: true,
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('全部<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    totalSize = 0;
                    minmoney = '1';
                    maxmoney = '999999';
                    sptypes = "";
                    maintypes = '';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            },
            {
                text: '现货',
                bold: true,
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('现货<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    totalSize = 0;
                    maintypes = '';
                    minmoney = '1';
                    maxmoney = '999999';
                    sptypes = '0,1,3,4,5,6,7,8';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            },
            {
                text: '预售',
                onClick: function () {
                    $('.option').removeClass('check');
                    $('#screenImg').attr('src','../../res/img/common/sx.png');
                    $('.screen').removeClass('common');
                    $(".all").html('预售<img src="../../res/img/common/downJt.png" />');
                    $('#usedList').empty();//清空容器
                    page = 0;
                    sptypes = '2';
                    maintypes = '';
                    minmoney = '1';
                    maxmoney = '999999';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            },
            // {
            //     text: '银币商品',
            //     onClick: function () {
            //         $('.option').removeClass('check');
            //         $('#screenImg').attr('src','../../res/img/common/sx.png');
            //         $('.screen').removeClass('common');
            //         $(".all").html('银币商品<img src="../../res/img/common/downJt.png" />');
            //         $('#usedList').empty();//清空容器
            //         mRefresh.resetUpLoading(true);
            //         page = 0;
            //         sptypes = '5';
            //         maintypes = '';
            //         loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
            //     }
            // },
            {
                text: '返回',
                onClick: function () {

                }
            }];
        $.modal({
            title: '请选择品类',
            verticalButtons: true,
            buttons:buttons1
        });
    });

    $('.priceSel').click(function () {
        $('#usedList').empty();//清空容器
        // mRefresh.resetUpLoading(true);
        var thisHtml = $(this).find('span').html();
        if(thisHtml === '价格升序'){
            sort = 'moneydesc';
            $('.newsBest').removeClass('common');
            $(this).find('span').addClass('common').html('价格降序');
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
        }else {
            sort = 'money';
            $('.newsBest').removeClass('common');
            $(this).find('span').addClass('common').html('价格升序');
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
        }
    });

    $('.newsBest').click(function () {
        $('#usedList').empty();//清空容器
        // mRefresh.resetUpLoading(true);
        sort = 'time';
        $('.selectCom span').removeClass('common');
        $(this).addClass('common');
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
    });

    //筛选模块
    $('.screen').click(function () {
        $('.screenIndex').show().removeClass('bounceOutRight').addClass('bounceInRight');
        $(".indexAlert").css("height",$(".alertContent").height())
    });
    $('.commodityType .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
        }else {
            $(this).addClass('check');
        }
    });

    $('.commodityAttribute .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
        }else {
            $(this).addClass('check');
        }
    });
    $('.commodityEncryption .option').click(function () {
        if($(this).hasClass('check')){
            $(this).removeClass('check');
            filter = 1
        }else {
            $(this).addClass('check');
            filter = 0
        }
    });

    $('.confirm').click(function () {
        $(".all").html('全部<img src="../../res/img/common/downJt.png" />');
        page = 0;
        totalSize = 0;
        var minMoney = $('#minmoney').val();
        var maxMoney = $('#maxmoney').val();
        if(Number(minMoney) > Number(maxMoney)){
            $.toast('最低价不能大于最高价');
            return
        }

        if(maxMoney > '99999'){
            $.toast('最高价不能大于99999');
            return
        }
        var commodityTypeArr = $(".commodityType .check");
        var commodityTypeArrPush = [];
        commodityTypeArr.each(function(){
            commodityTypeArrPush.push($(this).attr("optionCode"));
        });
        var commodityTypeStr = commodityTypeArrPush.toString(',');
        var commodityAttributeArr = $(".commodityAttribute .check");
        var commodityAttributeArrPush = [];
        commodityAttributeArr.each(function(){
            commodityAttributeArrPush.push($(this).attr("optionCode"));
        });
        var commodityAttributeStr = commodityAttributeArrPush.toString(',');
        if(commodityTypeStr === '' && commodityAttributeStr === '' && minMoney === '' && maxMoney === ''){
            $('#screenImg').attr('src','../../res/img/common/sx.png');
            $('.screen').removeClass('common');
        }else {
            $('.screen').addClass('common');
            $('#screenImg').attr('src','../../res/img/common/sxSec.png');
        }
        if(minMoney === ''){
            minmoney = '1'
        }else {
            minmoney = minMoney
        }

        if(maxMoney === ''){
            maxmoney = '99999'
        }else {
            maxmoney =maxMoney
        }
        $('#usedList').empty();//清空容器
        sptypes = commodityTypeStr;
        maintypes = commodityAttributeStr;
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key);
        $('.screenIndex').removeClass('bounceInRight').addClass('bounceOutRight');
    });

    $('.reset').click(function () {
        $('.option').removeClass('check');
        $('#usedList').empty();//清空容器;
        $('#screenImg').attr('src','../../res/img/common/sx.png');
        $('.screen').removeClass('common');
        sptypes = '';
        maintypes = '';
        totalSize = 0;
        page = 0;
        minmoney = '1';
        maxmoney = '999999';
        loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key);
    });

    $('.indexAlert').click(function () {
        $('.screenIndex').removeClass('bounceInRight').addClass('bounceOutRight');
    });


    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#usedList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
            }
        }
    });

    // 历史搜索
    function historySearch (){
        var data = {
            'markid':markid,
            'type':'xiandan'
        };
        getData('post','/niu/apiv3/search_resultlist.php',data,function (data) {
            console.log(data);
            var append = '';
            for (var i = 0; i < data.data.length && i < 20;i++){
                append += '<span>'+data.data[i].searchkey+'</span>'
            }

            $('#searchSpan').append(append);

            $('#searchSpan span').click(function () {
                key = $(this).html();
                $('#searchVal').val(key);
                $('.notSearch').hide();
                if(key.length === 8){
                    $('.notSearch,.indexContent').hide();
                    $('.XDCodeContent').show();
                    $('#XDCodeList').empty();//清空容器;
                    XDCode(key)
                }else {
                    $('.notSearch,.XDCodeContent').hide();
                    $('.indexContent').show();
                    $('#usedList').empty();//清空容器;
                    sptypes = '';
                    maintypes = '';
                    totalSize = 0;
                    page = 0;
                    minmoney = '1';
                    maxmoney = '999999';
                    loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
                }
            })
        })
    }

    historySearch();

    $('#goBack').click(function () {
        nativeCommand("dancool://activity=fin")
    });

    //搜索
    $('#searchBtn').click(function () {
        key = $('#searchVal').val();
        if(key === '' || key === undefined){
            $.toast('搜索内容不能为空');
            $('.indexContent,.nullContent,.XDCodeContent').hide();
            $('.notSearch').show();
            return
        }

        if(key.length === 8){
            $('.notSearch,.indexContent').hide();
            $('.XDCodeContent').show();
            $('#XDCodeList').empty();//清空容器;
            XDCode(key)
        }else {
            $('.notSearch,.XDCodeContent').hide();
            $('.indexContent').show();
            $('#usedList').empty();//清空容器;
            sptypes = '';
            maintypes = '';
            totalSize = 0;
            page = 0;
            minmoney = '1';
            maxmoney = '999999';
            loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
        }
    });
    $(".search").keypress(function(e){
        if(e.keyCode === 13) {
            key = $('#searchVal').val();
            if(key === '' || key === undefined){
                $.toast('搜索内容不能为空');
                $('.indexContent,.nullContent,.XDCodeContent').hide();
                $('.notSearch').show();
                return
            }

            if(key.length === 8){
                $('.notSearch,.indexContent').hide();
                $('.XDCodeContent').show();
                $('#XDCodeList').empty();//清空容器;
                XDCode(key)
            }else {
                $('.notSearch,.XDCodeContent').hide();
                $('.indexContent').show();
                $('#usedList').empty();//清空容器;
                sptypes = '';
                maintypes = '';
                totalSize = 0;
                page = 0;
                minmoney = '1';
                maxmoney = '999999';
                loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key)
            }
        }
    });


    //咸蛋码搜索
    function XDCode(key) {
        var data = {
            'markid':markid,
            'key':key,
        };
        getData('get','/niu/apiv3/xiandanv2_getmacodelist.php',data,function (data) {
            console.log(data);
            if(data.pagetotal === 0){
                $('.notSearch,.XDCodeContent').hide();
                $('.indexContent').show();
                $('#usedList').empty();//清空容器;
                sptypes = '';
                maintypes = '';
                totalSize = 0;
                page = 0;
                minmoney = '1';
                maxmoney = '999999';
                loadpagedata(minmoney,maxmoney,page,sptypes,maintypes,sort,key);
                return
            }else {
                $('.nullContent').hide();
            }
            var append = '';
            $(data.data).each(function () {
                append += '<li>' +
                    '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                    // '                   <input type="hidden" class="toyscount" value="'+this.toyscount+'"> '+
                    // '                   <input type="hidden" class="toysid" value="'+this.toysid+'"> '+
                    '                    <div class="pic">' +
                    '                        <img src="'+this.toyspic+'" alt="">' +
                    '                    </div>' +
                    '                    <div class="name">'+this.toysname+'</div>';
                if (this.ispw === '1'){
                    append +='<div class="leftTop">密码</div>'
                }
                if(this.toyscount === '1'){
                    append += '              <div class="price">' +
                        '                        <img src="'+this.userface+'" alt="">' +
                        '                        <span class="nickName">'+this.usernick+'</span>' +
                        '                        <span class=goldCoinNum>'+this.money2+'</span>' +
                        '                        <img class="goldCoin" src="../../res/img/common/goldCoin.png" alt="">' +
                        '                    </div>'
                }else {
                    append += '          <div class="price">' +
                        '                        <span class=goldCoinNum>'+this.money2+'</span>' +
                        '                        <img class="goldCoin" src="../../res/img/common/goldCoin.png" alt="">' +
                        '                    </div>'
                }
                append += '                </li>'

            });
            $('#XDCodeList').append(append);

            // $('#XDCodeList li').click(function () {
            //
            //     // window.location.href = 'dancool://xiandan='+id;
            // });
        })
    }

    $("#XDCodeList li").live("click", function(){
        var id = $(this).find('.id').val();
        window.location.href = 'dancool://fullweb='+urlView+'saltedEggV2/saltedDetail.html?id='+id;
    });

    //分类搜索
    function loadpagedata (minmoney,maxmoney,page,sptypes,maintypes,sort,key) {
        if(key === '' || key === undefined){
            return
        }
        var data = {
            'markid':markid,
            'minmoney':minmoney,
            'maxmoney':maxmoney,
            'page':page,
            'sptypes':sptypes,
            'maintypes':maintypes,
            'sort':sort,
            'key':key,
            'filter':filter,
        };
        getData('post','/niu/apiv3/xiandanv2_getlist.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                if(data.pagetotal === 0){
                    mRefresh.endUpLoading(true);
                    $('.minirefresh-hardware-speedup').hide();
                    $('.nullContent').show();
                    return
                }else {
                    // mRefresh.resetUpLoading(true);
                    $('.nullContent').hide();
                    $('.minirefresh-hardware-speedup').show();
                }
                $(data.data).each(function () {
                    if(this.showtype === 'xiandan'){
                        append += '<li class="XDItem">' +
                            '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                            '                   <input type="hidden" class="toyscount" value="'+this.toyscount+'"> '+
                            '                   <input type="hidden" class="toysid" value="'+this.toysid+'"> '+
                            '                    <div class="pic">' +
                            '                        <img src="'+this.pics+'" alt="">' +
                            '                    </div>' +
                            '                    <div class="name">'+this.name+'</div>';
                        if (this.ispw === '1'){
                            append +='<div class="leftTop">密码</div>'
                        }
                        append += '          <div class="price">' ;
                        if (this.isshow === '1'){
                            append +=   ' <span style="color: #ff0000;font-weight: 600" class=goldCoinNum>￥'+(this.moneyrmb)/100+'</span>' ;
                        } else {
                            append +=   '                        <img class="goldCoin" src="../../res/img/common/goldCoin.png" alt="">' +
                                '                        <span class=goldCoinNum>'+this.money2+'</span>' ;
                        }
                        append +=
                            '                    </div>'+
                            '                </li>'
                    }else {

                        append += '<li class="HPItem">' +
                            '                   <input type="hidden" class="id" value="'+this.id+'"> '+
                            '                   <input type="hidden" class="showtype" value="'+this.showtype+'"> '+
                            '                    <div class="pic">' +
                            '                        <img src="'+this.picbanner+'" alt="">' +
                            '                    </div>' +
                            '                    <div class="name">'+this.name+'</div>' +
                            '                    <div class="price1">' +
                            '                        <img class="goldCoin" src="../../res/img/common/HP.png" alt="">' +
                            '                        <span class=goldCoinNum>'+this.money3+'</span>' +
                            '                    </div>' +
                            '                    <div class="info1">可用'+this.money2+'金币代付</div>' +
                            '                </li>'
                    }

                });
                $('#usedList').append(append);


                totalSize = totalSize + 10;
                if (totalSize > data.datacount){
                    mRefresh.endUpLoading(true);
                } else{
                    mRefresh.resetUpLoading(true);
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        })
    }
    $("#usedList .XDItem").live("click", function(){
        var id = $(this).find('.id').val();
        var toyscount = $(this).find('.toyscount').val();
        var toysid = $(this).find('.toysid').val();
        if (toyscount === '1'){
            // window.location.href = 'dancool://xiandan='+id;
            window.location.href = 'dancool://fullweb='+urlView+'saltedEggV2/saltedDetail.html?id='+id;
        }else {
            // window.location.href = 'saltedEggGettoylist.html?toysid='+toysid
            window.location.href="dancool://web="+urlView+"saltedEggV2/saltedEggGettoylist.html?toysid="+toysid;
        }
    });

    $("#usedList .HPItem").live("click", function(){
        var id = $(this).find('.id').val();
        window.location.href = 'dancool://fullweb='+urlView+'HPStore/HPStoreDetail.html?poolid='+id+'&markid='+markid
    });
});
